package com.ligq.common.utils

object YuvUtils {
    fun nv21ToNv12(nv21: ByteArray): ByteArray {
        val size = nv21.size
        val nv12 = ByteArray(size)
        val yLen = size * 2 / 3
        System.arraycopy(nv21, 0, nv12, 0, yLen)
        var i = yLen
        while (i < size - 1) {
            nv12[i] = nv21[i + 1]
            nv12[i + 1] = nv21[i]
            i += 2
        }
        return nv12
    }

    /**
     * 后置摄像头采集数据旋转办法
     */
    fun portraitDataRawBack(input: ByteArray, width: Int, height: Int): ByteArray {
        val output = ByteArray(input.size)
        val yLen = width * height
        val uvHeight = height shr 1
        var k = 0
        for (j in 0 until width) {
            for (i in height - 1 downTo 0) {
                output[k++] = input[width * i + j]
            }
        }

        for (j in 0 until width) {
            for (i in uvHeight - 1 downTo 0 step 2) {
                output[k++] = input[yLen + width * i + j]
                output[k++] = input[yLen + width * i + j + 1]
            }
        }

        return output
    }

    /**
     * 前置摄像头采集数据旋转办法 YV12方式
     */
    fun portraitDataRawFront(src: ByteArray, width: Int, height: Int): ByteArray {
        var n = 0
        val hw = width / 2
        val hh = height / 2
        val des = ByteArray(src.size)
        //copy y
        for (j in width downTo 1) {
            for (i in 0 until height) {
                des[n++] = src[width * i + j]
            }
        }
        //copy u
        val yLen = width * height
        for (j in hw - 1 downTo 0) {
            for (i in 0 until hh) {
                des[n++] = src[yLen + hw * i + j] //ptemp[hw * i + j];
            }
        }

        //copy v
        for (j in hw - 1 downTo 0) {
            for (i in 0 until hh) {
                des[n++] =
                    src[yLen + yLen / 4 + hw * i + j] //ptemp[hw * i + j];
            }
        }
        return des
    }

    /**
     * 前置摄像头采集数据旋转办法 NV12方式
     */
    fun portraitDataRawFront2(src: ByteArray, width: Int, height: Int): ByteArray {
        var n = 0
        val uvHeight = height shr 1
        val des = ByteArray(src.size)
        //copy y
        for (j in width downTo 1) {
            for (i in 0 until height) {
                des[n++] = src[width * i + j]
            }
        }
        //copy u v
        val yLen = width * height
//        for (j in width - 1 downTo 0) {
//            for (i in 0 until uvHeight step 2) {
//                des[n++] = src[yLen + width * i + j]
//                des[n++] = src[yLen + width * i + j + 1]
//            }
//        }

        for (i in width / 2 - 1 downTo 0) {
            for (j in 0 until height / 2) {
                // 读取数据时, 要从 yLen 之后的数据开始遍历
                // 使用 mWidth 和 UVByteHeight 定位要遍历的位置
                // 拷贝 V 色彩值数据
                des[n++] = src[yLen + width / 2 * 2 * j + i]
                // 拷贝 U 饱和度数据
                des[n++] = src[yLen + width / 2 * 2 * j + i + 1]
            }
        }

        return des
    }

}